<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>
          <a-col :xs="24" :sm="12">
            <a-form-item label="工艺线路名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['flowName', validatorRules.flowName]" placeholder="请输入工艺线路名称"></a-input>
            </a-form-item>
          </a-col>

        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="工艺线路明细表" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="flowDetailTable.loading"
            :columns="flowDetailTable.columns"
            :dataSource="flowDetailTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :dragSort="true"
            :dragSortKey="sort"
            :rowSelection="true"
            :actionButton="true">
          </j-editable-table>
        </a-tab-pane>

      </a-tabs>

    </a-spin>
  </j-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'FlowModal',
    mixins: [JEditableTableMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          flowName: {
            rules: [
              { required: true, message: '请输入工艺线路名称!'},
              { validator: (rule, value, callback) => validateDuplicateValue('flow', 'flow_name', value, this.model.id, callback)},
            ]
          },
        },
        refKeys: ['flowDetail', ],
        tableKeys:['flowDetail', ],
        activeKey: 'flowDetail',
        // 工艺线路明细表
        flowDetailTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '当前工序',
              key: 'procName',
              type: FormTypes.popup,
              popupCode:"tj_proc",
              destFields:"procId,procName",
              orgFields:"id,name",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: 'procId',
              key: 'procId',
              type:"hidden"
            },

            {
              title: 'procName',
              key: 'procName',
              type:"hidden"
            },

            {
              title: 'beforeProcId',
              key: 'beforeProcId',
              type:"hidden"
            },

            {
              title: 'beforeProcName',
              key: 'beforeProcName',
              type:"hidden"
            },

            {
              title: 'nextProcId',
              key: 'nextProcId',
              type:"hidden"
            },

            {
              title: 'nextProcName',
              key: 'nextProcName',
              type:"hidden"
            }
          ]
        },
        url: {
          add: "/flow/flow/add",
          edit: "/flow/flow/edit",
          flowDetail: {
            list: '/flow/flow/queryFlowDetailByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'flowName')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.flowDetail.list, params, this.flowDetailTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          flowDetailList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'flowName'))
     }
    }
  }
</script>

<style scoped>
</style>